﻿Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms


Public Class Form2

    Private dataAdapter As New SqlClient.SqlDataAdapter()
    Private bindingSource1 As New BindingSource()

    Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        My.Settings.Form2Size = Me.Size
        My.Settings.Form2Pos = Me.Location
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Size = My.Settings.Form2Size
        FormPos(My.Settings.Form2Pos)
        Me.Location = My.Settings.Form2Pos
        Me.PictureBox1.Visible = False

        ' Bind the DataGridView to the BindingSource
        ' and load the data from the database.
        Me.DataGridView1.DataSource = Me.bindingSource1
        GetData("SELECT * FROM F0003 where CONF = 'N' order by timedate DESC")

    End Sub

    Function GetData(ByVal selectCommand As String)

        Try

            Dim sqlConnection1 As New System.Data.SqlClient.SqlConnection(My.Settings.CADISConnectionString)
            Dim cmd As New System.Data.SqlClient.SqlCommand
            Dim reader As SqlClient.SqlDataReader = Nothing

            cmd.CommandText = selectCommand
            cmd.Connection = sqlConnection1

            Me.dataAdapter = New SqlClient.SqlDataAdapter(cmd)
            Dim commandBuilder As New SqlClient.SqlCommandBuilder(Me.dataAdapter)

            Dim table As New DataTable()
            table.Locale = System.Globalization.CultureInfo.InvariantCulture
            Me.dataAdapter.Fill(table)
            Me.bindingSource1.DataSource = table



            ' Resize the DataGridView columns to fit the newly loaded content.
            Me.DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
        Catch ex As OleDbException

        End Try
        GetData = ""

    End Function

    Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
        Dim s As String
        Try
            s = DataGridView1.CurrentRow.Cells(1).Value.ToString
            If DataGridView1.CurrentRow.Cells(2).Value.ToString = "3" Then
                Dialog1.Label1.Text = s
                Dialog1.ShowDialog()
            End If

        Catch ex As Exception

        End Try

        Try
            s = DataGridView1.CurrentRow.Cells(1).Value.ToString
            If DataGridView1.CurrentRow.Cells(2).Value.ToString = "1" Then
                Dialog3.Label1.Text = s
                Dialog3.ShowDialog()
            End If

        Catch ex As Exception

        End Try




    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Try

            If DataGridView1.CurrentRow.Cells(5).Value.ToString = "N" Then
                Dim s As String
                s = DataGridView1.CurrentRow.Cells(0).Value.ToString
                ConfirmError(s)
                AddMessage("Error on: " & DataGridView1.CurrentRow.Cells(4).Value.ToString & "  is confirmed")

                'Application.DoEvents()

                Me.PictureBox1.Visible = True

                CheckAUF(GetAUFID(DataGridView1.CurrentRow.Cells(1).Value.ToString, DataGridView1.CurrentRow.Cells(2).Value.ToString), DataGridView1.CurrentRow.Cells(4).Value.ToString)


                If Not ActivError(DataGridView1.CurrentRow.Cells(4).Value.ToString) Then
                    ExportAUF(GetAUFID(DataGridView1.CurrentRow.Cells(1).Value.ToString, DataGridView1.CurrentRow.Cells(2).Value.ToString))
                    MsgBox("The order number: " & DataGridView1.CurrentRow.Cells(4).Value.ToString & "  is sended to M3")
                    AddMessage("The order number: " & DataGridView1.CurrentRow.Cells(4).Value.ToString & "  is sendet to M3")
                Else
                    MsgBox("The order number: " & DataGridView1.CurrentRow.Cells(4).Value.ToString & "  has activ errors")
                    AddMessage("The order number: " & DataGridView1.CurrentRow.Cells(4).Value.ToString & "  has activ errors")

                End If

                GetData("SELECT * FROM F0003 where CONF = 'N' order by timedate DESC")
            End If
        Catch ex As Exception
        End Try

        Me.PictureBox1.Visible = False


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
          GetData("SELECT * FROM F0003 order by timedate DESC")

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        GetData("SELECT * FROM F0003 where CONF = 'N' order by timedate DESC")

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        Try

            If DataGridView1.CurrentRow.Cells(5).Value.ToString = "N" Then
                Dim s As String
                s = DataGridView1.CurrentRow.Cells(0).Value.ToString
                ConfirmError(s)
                AddMessage("Error on: " & DataGridView1.CurrentRow.Cells(4).Value.ToString & "  is confirmed but not sended to M3")

                GetData("SELECT * FROM F0003 where CONF = 'N' order by timedate DESC")
            End If

        Catch ex As Exception

        End Try



    End Sub

    Private Sub BarButtonItem1_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick

        Try

            If DataGridView1.CurrentRow.Cells(5).Value.ToString = "N" Then
                Dim s As String
                s = DataGridView1.CurrentRow.Cells(0).Value.ToString
                ConfirmError(s)
                AddMessage("Error on: " & DataGridView1.CurrentRow.Cells(4).Value.ToString & "  is confirmed")

                'Application.DoEvents()

                Me.PictureBox1.Visible = True

                CheckAUF(GetAUFID(DataGridView1.CurrentRow.Cells(1).Value.ToString, DataGridView1.CurrentRow.Cells(2).Value.ToString), DataGridView1.CurrentRow.Cells(4).Value.ToString)


                If Not ActivError(DataGridView1.CurrentRow.Cells(4).Value.ToString) Then
                    ExportAUF(GetAUFID(DataGridView1.CurrentRow.Cells(1).Value.ToString, DataGridView1.CurrentRow.Cells(2).Value.ToString))
                    MsgBox("The order number: " & DataGridView1.CurrentRow.Cells(4).Value.ToString & "  is sended to M3")
                    AddMessage("The order number: " & DataGridView1.CurrentRow.Cells(4).Value.ToString & "  is sendet to M3")
                Else
                    MsgBox("The order number: " & DataGridView1.CurrentRow.Cells(4).Value.ToString & "  has activ errors")
                    AddMessage("The order number: " & DataGridView1.CurrentRow.Cells(4).Value.ToString & "  has activ errors")

                End If

                GetData("SELECT * FROM F0003 where CONF = 'N' order by timedate DESC")
            End If
        Catch ex As Exception
        End Try

        Me.PictureBox1.Visible = False

    End Sub

    Private Sub BarButtonItem2_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        GetData("SELECT * FROM F0003 order by timedate DESC")
    End Sub

    Private Sub BarButtonItem3_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick
        GetData("SELECT * FROM F0003 where CONF = 'N' order by timedate DESC")
    End Sub

    Private Sub BarButtonItem4_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem4.ItemClick

        Try

            If DataGridView1.CurrentRow.Cells(5).Value.ToString = "N" Then
                Dim s As String
                s = DataGridView1.CurrentRow.Cells(0).Value.ToString
                ConfirmError(s)
                AddMessage("Error on: " & DataGridView1.CurrentRow.Cells(4).Value.ToString & "  is confirmed but not sended to M3")

                GetData("SELECT * FROM F0003 where CONF = 'N' order by timedate DESC")
            End If

        Catch ex As Exception

        End Try


    End Sub

   
End Class